package com.wing.res.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.res.dao.ResProductDrawDao;
import com.wing.res.dao.ResProductMoldDrawDao;
import com.wing.res.model.entity.ResProductDraw;
import com.wing.res.model.entity.ResProductMoldDraw;
import com.wing.res.model.response.ResProductMoldDrawVo;
import com.wing.res.service.ResProductDrawService;
import com.wing.res.service.ResProductMoldDrawService;
import com.wing.web.utils.JwtUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Map;

/**
 * sku图纸-关联模具图纸-ServiceImpl
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-23 20:59
 **/
@Service
public class ResProductMoldDrawServiceImpl extends ServiceImpl<ResProductMoldDrawDao, ResProductMoldDraw> implements ResProductMoldDrawService {

    /**
     * 查询模具图纸列表
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ResProductMoldDrawVo> getListByMap(Page<ResProductMoldDrawVo> pg, Map map) {
        return this.baseMapper.getListByMap(pg,map);
    }

    /**
     * 作废图纸
     * @param id
     * @return
     */
    @Override
    public int cancellation(String id) {
        ResProductMoldDraw byId = this.getById(id);
        if (ObjectUtil.isNotEmpty(byId)){
            byId.setModifier(JwtUtils.getLoginUserId());
            byId.setUpdateTime(LocalDateTime.now());
            byId.setModifierName(JwtUtils.getLoginUsername());
            byId.setIsDeleted(true);
            boolean b = this.updateById(byId);
            if (b){
                return 1;
            }else{
                return 0;
            }
        }
        return 0;
    }

    /**
     * 恢复图纸
     * @param id
     * @return
     */
    @Override
    public int recover(String id) {
        ResProductMoldDraw byId = this.getById(id);
        if (ObjectUtil.isNotEmpty(byId)){
            byId.setModifier(JwtUtils.getLoginUserId());
            byId.setUpdateTime(LocalDateTime.now());
            byId.setModifierName(JwtUtils.getLoginUsername());
            byId.setIsDeleted(false);
            boolean b = this.updateById(byId);
            if (b){
                return 1;
            }else{
                return 0;
            }
        }
        return 0;
    }


    /**
     * 设为默认
     * @param id
     * @return
     */
    @Override
    public int setDefault(String id) {
        ResProductMoldDraw byId = this.getById(id);
        //设置为默认
        //将所有的设置为非默认
        Long drawId = byId.getDrawId();
        UpdateWrapper<ResProductMoldDraw> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("draw_id", drawId)
                .set("is_default", "0").set("modifier", JwtUtils.getLoginUserId())
                .set("modifier_name", JwtUtils.getLoginUsername()).set("update_time", LocalDateTime.now());
        update(updateWrapper);

        UpdateWrapper<ResProductMoldDraw> updateOne = new UpdateWrapper<>();
        updateOne.eq("id", id)
                .set("is_default", "1").set("modifier", JwtUtils.getLoginUserId())
                .set("modifier_name", JwtUtils.getLoginUsername()).set("update_time", LocalDateTime.now());
        update(updateOne);
        return 1;
    }

    /**
     * 添加模具图纸
     * @param resProductMoldDraw 模具图纸
     * @return
     */
    @Override
    public int addModelDraw(ResProductMoldDraw resProductMoldDraw) {
        resProductMoldDraw.setCreateTime(LocalDateTime.now());
        resProductMoldDraw.setModifier(JwtUtils.getLoginUserId());
        resProductMoldDraw.setModifierName(JwtUtils.getLoginUsername());
        resProductMoldDraw.setIsDeleted(false);
        resProductMoldDraw.setCode(UniqueIdGeneratorUtil.generateUniqueId("MJTZ", "yyyyMMddHHmmss", 1000, 9999));
        resProductMoldDraw.setDrawId(resProductMoldDraw.getDrawId());
        boolean save = this.save(resProductMoldDraw);
        if (save) {
            return 1;
        }
        return 0;
    }

    /**
     * 修改模具图纸
     * @param resProductMoldDraw
     * @return
     */
    @Override
    public int updateModelDraw(ResProductMoldDraw resProductMoldDraw) {
        ResProductMoldDraw byId = this.getById(resProductMoldDraw.getId());
        if (ObjectUtil.isNotEmpty(byId)){
            BeanUtil.copyProperties(resProductMoldDraw,byId);
            byId.setModifier(JwtUtils.getLoginUserId());
            byId.setModifierName(JwtUtils.getLoginUsername());
            byId.setModifier(JwtUtils.getLoginUserId());
            boolean updateById = this.updateById(byId);
            if (updateById) {
                return 1;
            }
        }
        return 0;
    }


}
